package com.destinomusical.epura.parser;

import java.io.IOException;
import java.util.ArrayList;

import org.xml.sax.SAXException;

import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.util.Log;
import android.util.Xml;

import com.destinomusical.epura.extra.Errores;
import com.destinomusical.epura.obj.Usuario;

public class ParserCliente extends BaseFeedParser {
	final ArrayList<Usuario> lista = new ArrayList<Usuario>();
	static final String ROOT = "RESULTS";

	public ParserCliente(String feedUrl) {
		super(feedUrl);
	}


	public ArrayList<Usuario> parse() {

		RootElement root = new RootElement(ROOT);
		
		final Usuario obj = new Usuario();
		
		Element row = root.getChild("ROW");
		row.getChild("cli_idr").setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String body) {
						System.out.println("saldo:" + body);
						obj.setId(body);
					}
				});
		
		row.getChild("cli_email").setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String body) {
						System.out.println("saldo:" + body);
						obj.setMail(body);
					}
				});
		row.getChild("cli_nombre").setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String body) {
						System.out.println("saldo:" + body);
						obj.setNombre(body);
					}
				});
		row.getChild("cli_apellidos").setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String body) {
						System.out.println("saldo:" + body);
						obj.setApellidos(body);
					}
				});
		row.getChild("cli_fecha").setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String body) {
						System.out.println("saldo:" + body);
						obj.setFecha(body);
					}
				});
		row.getChild("cli_udid_sesion").setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String body) {
						System.out.println("saldo:" + body);
						obj.setSesion(body);
					}
				});
		row.getChild("cli_password").setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String body) {
						System.out.println("saldo:" + body);
						obj.setPassword(body);
					}
				});
		row.getChild("suscripcion").setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String body) {
						System.out.println("saldo:" + body);
						obj.setSuscripcion(body);
					}
				});
		row.setEndElementListener(new EndElementListener(){

			@Override
			public void end() {
				lista.add(obj);
				
			}});
		
		
		try {
			Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
		} catch (IOException e) {
			Errores.NO_CONNECTION=1;
			Log.e("ParserNegocio",e.getMessage());
		} catch (SAXException e) {
			Log.e("ParserNegocio",e.getMessage());
		} catch (Exception e) {
			if(e.getLocalizedMessage()!= null){
				Log.e("ParserNegocio",e.getLocalizedMessage());
				Errores.NO_CONNECTION=1;
			}else
				Log.e("ParserNegocio","Unknown Exception");
		}
		return lista;
	}
}

